import wx
import wx.lib.platebtn as platebtn
import config

import os


class mconfig(wx.Panel):
    def __init__(self, parent, **k):
        wx.Panel.__init__(self, parent, **k)
        self._name = ""
        nm = wx.StaticBox(self, -1, '')
        bSizer   = wx.StaticBoxSizer(nm, wx.VERTICAL)
        bSizer7  = wx.BoxSizer( wx.VERTICAL )
        bSizer8  = wx.BoxSizer( wx.HORIZONTAL )
        bSizer9  = wx.BoxSizer( wx.HORIZONTAL )
        bSizer10 = wx.BoxSizer( wx.HORIZONTAL )

        self.mtitle   = wx.TextCtrl(self, style=wx.TE_CENTRE)
        self.bokid    = wx.TextCtrl(self, style=wx.TE_CENTRE|wx.TE_READONLY)
        self.startime = wx.TextCtrl(self, style=wx.TE_CENTRE|wx.TE_READONLY)
        self.m_copy   = wx.Button(self, label="复制")
        self.m_rnme = wx.Button(self, label="重命名")
        self.m_open   = wx.Button(self, label="打开笔记")

        bSizer8.Add(wx.StaticText(self, label=u"笔记名："), wx.SizerFlags().Border().Center())
        bSizer8.Add(self.mtitle, wx.SizerFlags(1).Center())
        bSizer8.Add(self.m_rnme, 0, wx.RIGHT, 5)

        bSizer7.Add(bSizer8, 0, wx.EXPAND, 5)

        bSizer9.Add(wx.StaticText(self, label=u"笔记ID："), wx.SizerFlags().Border().Center())
        bSizer9.Add(self.bokid, wx.SizerFlags(1).Center())
        bSizer9.Add(self.m_copy , 0, wx.RIGHT, 5)
        
        bSizer7.Add(bSizer9, 0, wx.EXPAND, 5 )

        bSizer10.Add(wx.StaticText(self, label=u"笔记戳："), wx.SizerFlags().Border().Center())
        bSizer10.Add(self.startime , wx.SizerFlags(1).Center())
        bSizer10.Add(self.m_open , 0, wx.RIGHT, 5)

        bSizer7.Add(bSizer10, 0, wx.EXPAND, 5 )

        bSizer12 = wx.BoxSizer( wx.VERTICAL )
        self.m_richText4 = wx.richtext.RichTextCtrl(self, style=wx.richtext.RE_READONLY)
        bSizer12.Add(self.m_richText4, 1, wx.EXPAND |wx.ALL, 5 )
        bSizer7.Add(bSizer12, 1, wx.EXPAND, 5 )
        bSizer.Add(bSizer7, 1, wx.EXPAND, 10 )
        self.SetSizer( bSizer )
        self.mtitle.Enable(False)
        #self.bokid.Enable(False)
        #self.startime.Enable(False)
        
        self.Layout()
        #print(dir(self.m_copy))
        self.m_copy.SetBackgroundColour(wx.Colour(240,240,240))
        self.m_rnme.SetBackgroundColour(wx.Colour(240,240,240))
        self.m_open.SetBackgroundColour(wx.Colour(240,240,240))
        #self.m_copy.SetLabelColor(wx.CYAN)
        #self.m_open.Bind(wx.EVT_BUTTON, self.on_button)
        self.m_rnme.Bind(wx.EVT_BUTTON, self.on_button)

    def on_button(self, eve):
        if self.m_rnme.GetLabel() == "重命名":
            self.mtitle.Enable(True)
            self.m_rnme.SetLabel("完成")
        else:
            old_path = os.path.join(config.M_PATH, self.bokid.GetValue(), self._name)
            new_path = os.path.join(config.M_PATH, self.bokid.GetValue(), self.mtitle.GetValue())
            print(old_path, new_path)
            if old_path != new_path:
                os.rename(old_path, new_path)
                self._name = self.mtitle.GetValue()
            self.mtitle.Enable(False)
            self.m_rnme.SetLabel("重命名")

    def _on_setdata(self, name, _id, startime):
        self.mtitle.SetValue(name)
        self.bokid.SetValue(_id)
        self.startime.SetValue(startime)

    def on_setdata(self, data):
        #print(path)
        self._name = data["name"]
        wx.CallAfter(self._on_setdata, data["name"], data["id"], data["startime"])

